package util.web;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.WeiboAPI.FEATURE;
import com.weibo.sdk.android.net.RequestListener;

public class WeiboDialogListener implements WeiboAuthListener {
		private Handler handler = null; 
		private String usename = null;
		
		public WeiboDialogListener(Handler handler, String usename){
			this.handler = handler;
			this.usename = usename;
		}
		
		@Override
		public void onCancel() {
			sendMessage(SinaWeiboHelper.LOGINT_CANCEL, null);
		}

		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			Oauth2AccessToken accessToken = new Oauth2AccessToken(token, expires_in);
			if (accessToken.isSessionValid()) {
				StatusesAPI statusesAPI = new StatusesAPI(accessToken);
				long zero = 0;
				statusesAPI.userTimeline(usename, zero, zero, 1, 1, false, FEATURE.ALL, false, new MyRequestListener(handler));
			}else{
				sendMessage(SinaWeiboHelper.LOGIN_FAIL, null);
			}
		}

		@Override
		public void onError(WeiboDialogError arg0) {
			sendMessage(SinaWeiboHelper.COMMON_ERROR, null);
		}

		@Override
		public void onWeiboException(WeiboException arg0) {
			sendMessage(SinaWeiboHelper.NETWORK_ERROR, null);
		}
		
		private void sendMessage(int what, Object obj){
			Message msg = new Message();
			msg.what = what;
			msg.obj = obj;
			handler.sendMessage(msg);
		}
		
		private class MyRequestListener implements RequestListener{
			private Handler handler = null;
			
			public MyRequestListener(Handler handler) {
				this.handler = handler;
			}
			
			@Override
			public void onComplete(String arg0) {
				System.out.println("Complete");
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(arg0);
					System.out.println(arg0);
					JSONArray statusesArr = jsonObject.getJSONArray("statuses");
					JSONObject statusesObj = statusesArr.getJSONObject(0);
					String weibo = statusesObj.getString("text");
					sendMessage(SinaWeiboHelper.SUCCESS, weibo);
				} catch (JSONException e) {
					sendMessage(SinaWeiboHelper.COMMON_ERROR, null);
				}
			}

			@Override
			public void onError(WeiboException arg0) {
				System.out.println("OnError");
				System.out.println(arg0.getMessage());
				sendMessage(SinaWeiboHelper.COMMON_ERROR, null);
			}

			@Override
			public void onIOException(IOException arg0) {
				System.out.println("OnException");
				sendMessage(SinaWeiboHelper.NETWORK_ERROR, null);
			}
			
			private void sendMessage(int what, Object obj){
				Message msg = new Message();
				msg.what = what;
				msg.obj = obj;
				handler.sendMessage(msg);
			}
			
		}
}
